﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;

namespace ImageAppDataModels
{
    [Serializable]
    public class SavedMetaData
    {
        private List<double> _ratings;

        public string FileName { get; set; }
        
        public List<double> Ratings
        {
            get
            {
                if (_ratings == null)
                    Interlocked.CompareExchange(ref _ratings, new List<double>(), null);

                return _ratings;
            }
            set
            {
                _ratings = value;
            }
        }
    }

    [Serializable]
    public class AllMetaData : ThreadSafeLookup<string, SavedMetaData>
    {
        [NonSerialized]
        public static ICoreDataProvider DefaultDataProvider;

        [NonSerialized]
        public ICoreDataProvider _DataProvider;

        public ICoreDataProvider DataProvider
        {
            get
            {
                if (_DataProvider == null)
                {
                    _DataProvider = DefaultDataProvider;
                }
                return _DataProvider;
            }
            set
            {
                if (_DataProvider != value)
                {
                    _DataProvider = value;
                    // HEREHERE?
                }
            }
        }

        [NonSerialized]
        public bool _dirty;

        public void SaveIfDirty(string path)
        {
            if (_dirty)
            {
                _dirty = false;
                SaveToDisk(path);
            }
        }

        public void SaveToDisk(string path)
        {
            DataProvider.WriteAllBytes(path, this.SerializeToCompressedBinary(true));
        }

        public SavedMetaData GetMetaData(string fileName)
        {
            SavedMetaData found = this[fileName];
            
            if (found == null)
            {
                found = new SavedMetaData() { FileName = fileName };
                this.AddOrSet(fileName, found);
                _dirty = true;
            }

            return found;
        }

        public void AddMetaData(string fileName, double rating)
        {
            SavedMetaData found = GetMetaData(fileName);
            found.Ratings.Add(rating);
            Console.WriteLine(string.Format("Added rating of {0} to file {1}", rating, fileName));
            _dirty = true;
        }

        public static AllMetaData ReadFromDisk(string path, ICoreDataProvider dataProvider)
        {
            byte[] bytes = dataProvider.ReadAllImageBytes(path);
            AllMetaData result = bytes.DeserializeFromCompressedBinary(false) as AllMetaData;
            return result;
        }
    }
}

